<?php
namespace Home\Controller;
use Think\Controller;
class GoodsController extends CommonController {
	protected $user_id=0;
    protected $info;
    protected $goods_id;
 /**
     * 构造函数
     */
    public function __construct() {
        parent::__construct();

       $this->info=$this->common_info;
       if($this->info['user_id'])
       {
         $this->user_id=$this->info['user_id'];
       }

      /*************测试数据********************开始***/
      // $_POST['id']='18';
      /*************测试数据********************结束***/
       $this->goods_id =I('post.id',0, 'int');
       if(!$this->goods_id)$this->goods_id =I('get.id',0, 'int');//get传值

    }

    //获取商品分享链接
    public function get_goods_share_url()
    {

      $goods_id=$this->goods_id;
      if(!$goods_id)return_data(20001,'商品ID不能为空');
      $info=$this->info;
      $mshop_id=$info['mshop_id'];
      $user_id=$info['user_id'];
      
      //同--推广链接
      $str='';
      if($info['utype']==2){

        $str="&user_pid=$user_id&mshopNo=$mshop_id";
      }


      $re_data['share_url']=get_path_url()."mobile/index.php?m=default&c=goods&a=index&id=$goods_id".$str;
      // $share_url=M('shop_config')->where("code='share_url'")->getField('value');

      
      return_data(0,'商品分享链接',$re_data);

    }


    //选择商品规格
	 public function get_properties()
	 {
	   	 $goods_id=$this->goods_id;
       if(!$goods_id)return_data(20001,'商品ID为空'); 

	  	$properties = D('Goods')->get_goods_properties($goods_id);

      

      $result=array_values($properties['spe']); //返回所有键值 

	 	  if($result)
	      {
	        return_data(0,'商品规格下拉成功',$result);
	      }
	      else
	      {
	         return_data(0,'商品规格为空',$result);
	      }
	 }
     /**
     * 获取某个商品的价格
     */
    public function get_goods_price() {
           

          /*************测试数据********************开始***/
          // $_POST['goods_id']=7;
          // $_POST['num']=2;
          // $_POST['attr_id']=array('60','58');

           // print_r(json_encode($_POST));exit();
          /*************测试数据********************结束***/


        $goods_id=I('post.goods_id',0,'int');
        $num=I('post.num',1,'int');
        $attr_id=I('post.attr_id','');

        $final_price=0;
        $list=array();

        if(is_array($attr_id)&&$goods_id&&!empty($attr_id))$final_price=D('GoodsBase')->get_final_price($this->info,$goods_id, $num, true, $attr_id);
        else if($goods_id)$final_price=D('GoodsBase')->get_final_price($this->info,$goods_id, $num);

        $list['final_price']=$final_price;
        $list['formated_final_price']=price_format($final_price);
        if($final_price>0)return_data(0,'显示最终价格成功',$list);
        else return_data(20002,'显示最终价格失败');
      }

        /**
     *  商品详情页
     */
    public function index() {
      /*************测试数据********************开始***/
      // $_POST['last']='1';
      // $_POST['amount']='5';

      //  print_r(json_encode($_POST));exit();
      /*************测试数据********************结束***/

      $goods_id=$this->goods_id;
      if(!$goods_id)return_data(20001,'商品ID为空'); 

	    // 获得商品的信息
	    $goods = D('Goods')->get_goods_info($goods_id,$this->info);

	    //取消商品图片
	    unset($goods['goods_img']);

	    if(!$goods['id'])return_data(20002,'商品ID不合法');

	    $imgs=D('GoodsBase')->get_goods_gallery($goods_id);
        $goods['imgs']=$imgs;

	    
        // 更新点击次数
        $where['goods_id'] = $goods_id;
        M('goods')->where($where)->setInc('click_count',1);
          
          if($goods)
          {
            return_data(0,'商品详情显示成功',$goods);
          }
          else
          {
             return_data(0,'商品详情显示失败',$goods);
          }


    }
}